<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<!--bootstrap-toastr-->
	<script type="text/javascript" src="/bootstrap/toastr/toastr.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/bootstrap/toastr/toastr.css">

	<script>
		$(function () {
			getListSpecification();
		})
		
		function getListSpecification() {
			$.ajax({
				url : "/specification/getListSpecifications",
				dataType : "json",
				type : "post",
				data : {pageNumber : $("#pageNumber").val(),"specName" : $("#searchSpecName").val()},
				success : function (returnData) {
					var result = returnData.data.list;
					var table = '';
					for (var i = 0; i < result.length; i++) {
						table += '<tr>'
						table += '<td><input name="box" value="'+ result[i].id +'" type="checkbox" ></td>'
						table += '<td>'+ result[i].id +'</td>'
						table += '<td>'+ result[i].specName +'</td>'
						table += '<td class="text-center"> '
						table += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" onclick="toUpdateSpecification('+result[i].id+')">修改</button> '
						table += "</td>"
						table += "</tr>"
					}

					$("#listSpec").html(table);

					$("#showCount").html(returnData.data.total)
					$("#showPage").html(returnData.data.pages)

					$("#totalPage").val(returnData.data.pages)
					$("#totalCount").val(returnData.data.total)
					$("#pageNumber").val(returnData.data.pageNumer)

					var rowPages = ""
					for (var j = 1;j <= returnData.data.pages;j++){
						rowPages += "<li>"
						rowPages += "<a href='javascript:changePageNumber("+ j +")'>"+ j +"</a>"
						rowPages += "</li>"
						$("#pages").html(rowPages)
					}
				},
				error : function () {

				}
			})
		}

		function getPage(flag) {
			var pageNumber = parseInt($("#pageNumber").val());
			var totalPage = parseInt($("#totalPage").val());

			if (flag == 1){
				if (pageNumber <= 1){
					toastr.warning("已经是第一页");
					return;
				}
				$("#pageNumber").val(1);
			}else if (flag == 2) {
				if (pageNumber <= 1){
					toastr.warning("已经是第一页");
					return;
				}
				$("#pageNumber").val(pageNumber - 1);
			}else if (flag == 3){
				if (pageNumber >= totalPage){
					toastr.warning("已经是最后一页");
					return;
				}
				$("#pageNumber").val(pageNumber + 1);
			} else if (flag == 4) {
				if (pageNumber >= totalPage){
					toastr.warning("已经是最后一页");
					return;
				}
				$("#pageNumber").val(totalPage);
			}
			getListSpecification();
		}

		function changePageNumber(obj) {
			$("#pageNumber").val(obj);
			getListSpecification();
		}

		function reload() {
			window.location.reload();

		}

		//删除
		function deleteBatch() {
			if (confirm("确定删除吗？")) {
				var names = $("[name=box]:checked");
				var ids = [];
				$(names).each(function (i, e) {
					ids.push(e.value)
				});
				if (ids <= 0) {
					toastr.warning("请选择要删除的规格")
					return;
				}
				$.ajax({
					url : "/specification/deleteBatch",
					dataType : "json",
					type : "post",
					data : {ids : ids},
					success : function (resultData) {
						if (resultData.code == 200) {
							toastr.success(resultData.message);
						}else {
							toastr.error(resultData.message)
						}
						setTimeout(function(){
									reload();
								},1000
						);
					},
					error : function () {
						toastr.error("系统错误")
					}
				})
			}
		}

	</script>


</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addSpecModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input  type="text" id="searchSpecName">
									<button class="btn btn-default" onclick="getListSpecification()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="listSpec">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<div align="center">
								<nav aria-label="Page navigation">
									<input type="hidden" id="pageNumber" name="pageNumber">
									<input type="hidden" id="totalPage">
									<div class="page_num_inf">
										<i></i> 共
										<span id="showPage"></span>页<span id="showCount"></span>条
									</div>
									<ul class="pagination">
										<li>
											<a href="javascript:getPage(1);" aria-label="Previous">
												<span aria-hidden="true">首页</span>
											</a>

											<a href="javascript:getPage(2);" aria-label="Previous">
												<span aria-hidden="true">&laquo;上一页</span>
											</a>
										</li>
									</ul>
									<ul class="pagination" id="pages">

									</ul>
									<ul class="pagination">
										<li>
											<a href="javascript:getPage(3);" aria-label="Next">
												<span aria-hidden="true">下一页&raquo;</span>
											</a>
											<a href="javascript:getPage(4);" aria-label="Previous">
												<span aria-hidden="true">尾页</span>
											</a>
										</li>
									</ul>
								</nav>
							</div>
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 增加窗口 -->
<div class="modal fade" id="addSpecModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myAddModalLabel">增加规格</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input id="specName" name="specName" class="form-control" placeholder="规格名称" >  </td>
		      	</tr>
			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" class="btn btn-default" title="新建" onclick="add_btn()" ><i class="fa fa-file-o"></i> 新增规格选项</button>
                  
             </div>
			 
			 <table class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                        <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
						</tr>
                    </thead>
                    <tbody id="table">
                      <tr id="trOne">
						<td>
							<input name="optionName"  class="form-control" placeholder="规格选项">
						</td>
						<td>
							<input name="orders" class="form-control" placeholder="排序">
						</td>
						<td>
							<button type="button" class="btn btn-default" title="删除" onclick="del_btn(this)"><i class="fa fa-trash-o"></i> 删除</button>
						</td>
                      </tr>
					  <tr>
						  <td>
							  <input name="optionName" class="form-control" placeholder="规格选项">
						  </td>
						  <td>
							  <input name="orders" class="form-control" placeholder="排序">
						  </td>
						  <td>
							  <button onclick="del_btn(this)" type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
					  <tr>
						  <td>
							  <input name="optionName" class="form-control" placeholder="规格选项">
						  </td>
						  <td>
							  <input name="orders" class="form-control" placeholder="排序">
						  </td>
						  <td>
							  <button onclick="del_btn(this)" type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
                    </tbody>
			  </table>
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addSpecification()">添加</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 修改窗口 -->
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">修改规格</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>规格名称</td>
						  <input type="hidden" name="id" id="id">
						  <td><input id="updateSpecName" name="specName"  class="form-control" placeholder="规格名称" >  </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="新建" onclick="add_btn1()" ><i class="fa fa-file-o"></i> 新增规格选项</button>

				  </div>

				  <table class="table table-bordered table-striped table-hover dataTable" id="td">
					  <thead>
					  <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
					  </tr>
					  </thead>
					  <tbody id="updateTable">

					  </tbody>s
				  </table>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateSpecification()">修改</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>
<script>

	function add_btn(){
		var $td = $("#trOne").clone();
		$("#table").append($td);
	}

	function add_btn1(){
		var $td = $("#trTwo").clone();
		$("#updateTable").append($td);
	}

	function del_btn(o) {
		var tr = o.parentNode.parentNode;
		var tb = tr.parentNode;
		tb.removeChild(tr);
	}
	
	function addSpecification() {
		var trs = $("#table").children("tr");
		var optionList =[];
		for (var i = 0; i < trs.length; i++) {
			var tds = $(trs[i]).find("td");
			var optionValue = $(tds[0]).find("input").val();
			var orderValue = $(tds[1]).find("input").val();
			var options = {"optionName" : optionValue,"orders" : orderValue};
			optionList.push(options);
		}

		var str = JSON.stringify(optionList);

		$.ajax({
			url : "/specification/insertSpecification",
			dataType : "json",
			type : "post",
			data : {"jsonStr" : str,"specName" : $("#specName").val()},
			success : function (returnData) {
				if (returnData.code == 200) {
					toastr.success(returnData.message)
				}else {
					toastr.error(returnData.message)
				}
				setTimeout(function(){
							reload();
						},1000
				);
			},
			error : function () {
				toastr.error("系统错误")
			}
		})
	}
	function toUpdateSpecification(id) {
		$.ajax({
			url : "/specification/getSpecificationById/" + id,
			dataType : "json",
			type : "post",
			success : function (returnData) {
				if (returnData.code == 200){
					$("#id").val(returnData.data.id);
					$("#updateSpecName").val(returnData.data.specName);

					var options = returnData.data.options;
					console.log(options);
					var tbody = "";
					for (var i = 0; i < options.length; i++) {
						tbody += "<tr id='trTwo'>"
						tbody += "<td>"
						tbody += "<input name='optionName' value='"+ options[i].optionName +"' class='form-control' placeholder='规格选项'>"
						tbody += "</td>"
						tbody += "<td>"
						tbody += "<input name='orders' value='"+ options[i].orders +"' class='form-control' placeholder='排序'> "
						tbody += "</td>"
						tbody += "<td>"
						tbody += "<button  type='button' class='btn btn-default' title='删除' onclick='del_btn(this)'><i class='fa fa-trash-o'></i> 删除</button>"
						tbody += "</td>"
						tbody += "</tr>"
					}
					$("#updateTable").html(tbody)
				}else {
					toastr.error(returnData.message)
				}

			},
			error : function () {
				toastr.error("系统错误")
			}
		})
	}

	function updateSpecification() {
		var trs = $("#updateTable").children("tr");
		var optionList = [];
		for (var i = 0; i < trs.length; i++) {
			var tds = $(trs[i]).find("td");
			var optionValue = $(tds[0]).find("input").val();
			var orderValue = $(tds[1]).find("input").val();
			var options = {"optionName" : optionValue,"orders" : orderValue};
			optionList.push(options);
		}
		var str = JSON.stringify(optionList);

		$.ajax({
			url : "/specification/updateSpecification",
			dataType : "json",
			type : "post",
			data : {"jsonStr" : str,"specName" : $("#updateSpecName").val(),"id" : $("#id").val()},
			success : function (resultData) {
				if (resultData.code == 200) {
					toastr.success(resultData.message)
					setTimeout(function(){
								reload();
							},1000
					);
				}else {
					toastr.error(resultData.message)
					setTimeout(function(){
								reload();
							},1000
					);
				}
			},
			error : function () {
				toastr.error("系统错误")
			}
		})
	}
</script>